<?php
namespace zake\foundation\local;

use zake\foundation\able\stream\ResourceAble;


/**
 * Class LocalResource
 * 
 * @author liu.bin 2020-05-07 08:40
 */
abstract class Local implements ResourceAble
{

    /**
     * @var string 文件路劲
     */
    private $resourcePath = "";

    /**
     * @var null | resource
     */
    private $resource = null;

    // 浏览目录默认过滤的文件
    protected $filterFileList = ['__MACOSX','.DS_Store','.gitkeep'];

    // 过滤文件
    protected $mustFileList = ['.','..'];


    /**
     * @param string $filePath 文件全路径
     * File constructor.
     */
    public function __construct($resourcePath)
    {
        $this->resourcePath = $resourcePath;
    }



    /**
     * local path
     * @return string
     *
     * @author liu.bin 2020-05-07 08:43
     */
    public function getLocalPath()
    {
        return $this->resourcePath;
    }

    /**
     * 获取fd
     * @return int
     *
     * @author liu.bin 2020-04-01 00:05
     */
    public function getFd()
    {
        return 0;
    }


    /**
     * 获取资源
     * @param string $mode
     * @return resource
     *
     * @author liu.bin 2020-03-31 23:51
     */
    public function getResource($mode="w+")
    {
        if(is_null($this->resource)){
            $this->resource = fopen($this->resourcePath,$mode);
        }
        return $this->resource;
    }



    /**
     * 关闭
     * @return bool
     *
     * @author liu.bin 2020-04-26 16:11
     */
    public function close($callBack = null)
    {
        $this->closeResource();
        if($callBack instanceof \Closure){
            $callBack();
        }
        return true;
    }


    /**
     * 关闭资源
     *
     * @return bool
     * @author liu.bin 2020-03-31 17:03
     */
    public function closeResource()
    {

        if(!$this->isCloseResource()){
            fclose($this->getResource());
            $this->resource = null;
        }
        return true;
    }

    /**
     * 资源是否被关闭
     * 一般为连接方关闭
     *
     * @return bool
     *
     * @author liu.bin 2020-04-25 18:49
     */
    public function isCloseResource()
    {
        return is_resource($this->resource) ? true : false;
    }


    /**
     * 文件或目录 是否存在
     * @return bool
     *
     * @author liu.bin 2020-05-07 08:44
     */
    public function exists()
    {
        return file_exists($this->resourcePath);
    }


    /**
     * 是否目录
     * @return bool
     *
     * @author liu.bin 2020-05-07 08:44
     */
    public function isDirectory()
    {
        return is_dir($this->resourcePath);
    }

    /**
     * 是否文件
     * @return bool
     *
     * @author liu.bin 2020-05-07 08:44
     */
    public function isFile()
    {
        return is_file($this->resourcePath);
    }



    /**
     * 获取父级对象
     *
     * @author liu.bin 2020-05-07 10:36
     */
    public function getParent()
    {
        $parent = dirname($this->resourcePath);
        return Dir::create($parent);
    }


    /**
     * 删除当前文件或目录
     *
     * 如果是目录的话
     *  $recursive = false  有子文件则删除失败
     *  $recursive = true   递归删除目录和子文件
     *
     * @param bool $recursive
     * @return bool
     *
     * @author liu.bin 2019-07-13 11:11
     */
    public function delete($recursive=false)
    {
        if(!$this->exists()){
            return true;
        }

        // 文件
        if($this instanceof File && $this->isFile()){
            return @unlink($this->getLocalPath());
        }

        // 目录
        if($this instanceof Dir && $this->isDirectory()){
            if($recursive){
                if(!$this->empty()){
                    return false;
                }
                return @rmdir($this->getLocalPath());
            }else{
                if(!$this->isEmpty(false)){
                    return false;
                }
                return @rmdir($this->getLocalPath());
            }
        }
        return false;
    }

    abstract function toMap();
}
